<template>
  <div>
    <a-modal title="导出核算表" :maskClosable="false" :visible="visible" @ok="handleOk" @cancel="handleCancel" :confirmLoading="confirmLoading">
      <a-spin :spinning="confirmLoading">
        <j-form-container :disabled="false">
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
            <a-row :gutter="48">
              <a-col :span="24">
                <a-form-model-item label="导出的核算表月份" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="salaryPeriod">
                  <j-month-date
                    placeholder="请选择导出的核算表月份"
                    v-model="model.salaryPeriod"
                    :disabledDate="disabledDate"
                    style="width: 100%;" />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="社保单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="insuranceUnit">
                  <select-Company-input placeholder="请选择社保单位" urlList="/api/erp/files/employee/erpEmployeePostChangeRecord/queryCompanyByPost" v-model="model.insuranceUnit" @back="handleCompanyBack" />
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </j-form-container>
      </a-spin>
    </a-modal>
  </div>
</template>
<script>
import SelectCompanyInput from '@/views/scy/components/SelectCompanyInput' // 公司
import moment from 'moment'
import { downFile } from '@/api/manage'
export default {
  name: 'ErpSalaryAccountingRecordExportModal',
  components: {
    SelectCompanyInput
  },
  data() {
    return {
      model: {
        salaryPeriod: '',
        insuranceUnit: '',
        fileName: '' // 选择的公司名称
      },
      labelCol: {
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 24 }
      },
      validatorRules: {
        salaryPeriod: [
          { required: true, message: '请选择导出的核算表月份!' }
        ],
        insuranceUnit: [
          { required: true, message: '请选择社保单位!' }
        ]
      },
      url: {
        exportXlsUrl: '/api/erp/salacct/erpSalaryAccountingRecord/exportXls'
      },
      visible: false,
      confirmLoading: false
    };
  },
  methods: {
    // 禁用
    disabledDate(current) {
      const date = new Date()
      let month = date.getMonth()
      // 禁用当前月份和之后的所有月份
      return current.month() >= month
    },
    // 打开弹窗
    showModal() {
      this.model = {
        salaryPeriod: moment().subtract(1, 'month').format('YYYY-MM'),
        insuranceUnit: '',
        fileName: ''
      }
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.form.clearValidate();
      })
    },
    handleCompanyBack (val) {
      this.model.fileName = val[0].fileName
    },
    handleOk(e) {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.confirmLoading = true;
          this.handleExportXls()
        }
      });
    },
    handleExportXls(fileName) {
      if (!fileName || typeof fileName !== 'string') {
        fileName = moment(this.model.salaryPeriod).format('YYYY年MM月') + this.model.fileName + '工资核算表'
      }
      let param = {
        salaryPeriod: this.model.salaryPeriod,
        insuranceUnit: this.model.insuranceUnit,
        insuranceUnitName: this.model.fileName
      }
      downFile(this.url.exportXlsUrl, param).then(data => {
        if (!data) {
          this.$message.warning('文件下载失败')
          return
        }
        if (typeof window.navigator.msSaveBlob !== 'undefined') {
          window.navigator.msSaveBlob(
            new Blob([data], {
              type: 'application/vnd.ms-excel'
            }),
            fileName + '.xls'
          )
        } else {
          let url = window.URL.createObjectURL(
            new Blob([data], {
              type: 'application/vnd.ms-excel'
            })
          )
          let link = document.createElement('a')
          link.style.display = 'none'
          link.href = url
          link.setAttribute('download', fileName + '.xls')
          document.body.appendChild(link)
          link.click()
          document.body.removeChild(link) // 下载完成移除元素
          window.URL.revokeObjectURL(url) // 释放掉blob对象
        }
        this.visible = false;
      }).finally(() => {
        this.confirmLoading = false
      })
    },
    handleCancel(e) {
      this.visible = false;
    }
  }
};
</script>
